package uit.edu.vn.Utility;

import java.net.HttpURLConnection;
import java.net.URL;

import android.content.Context;
import android.net.ConnectivityManager;
import android.util.Log;
import android.widget.Toast;

public class CheckInternet {

	 private Context context;
	 private ConnectivityManager connMg;
	 public CheckInternet(Context context)
	 {
		 this.context=context;
	 }
	 public Boolean checkStatus()
	 {
		  connMg =(ConnectivityManager)context.getSystemService(context.CONNECTIVITY_SERVICE);
		  android.net.NetworkInfo wifi= connMg.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		  android.net.NetworkInfo mobile = connMg.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		  if (wifi.isAvailable() ) 
		  {
//			Toast.makeText(context, "Dang co ket noi wifi", Toast.LENGTH_SHORT).show();
			return true;
		  }
		  else if(mobile.isAvailable())
		  {
			  
//			  Toast.makeText(context, "Dang co ket noi GPRS", Toast.LENGTH_SHORT).show();
			  return true;
		  }
		  
		  return false;
		  
	 }
	 public Boolean checkConnectToServer()
	 {
		 try {
			 URL url = new URL("http://www.google.com.vn/");
			 HttpURLConnection urlConnection=(HttpURLConnection) url.openConnection();
			 urlConnection.getConnectTimeout();
			 int respone=urlConnection.getResponseCode();
			 
			 Log.d("check internet", String.valueOf(respone));
			 if (respone==HttpURLConnection.HTTP_ACCEPTED||respone==HttpURLConnection.HTTP_OK)
			 {
				return true;
			 }
			 else 
			 	{
				 return false;
			 	}
		 	} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();

			return false;
		}
	 }
}
